const app = getApp();
var api = require('../../../config/api.js');//接口api地址

// 手机号返回的mssion_id
let mssion_id = null,
  outInter = null,
  outTime = 0;
Page({
  data: {
    sendState: "发送",
    phone:'',
    password:'',
  },
  onLoad:function(op){
    let token = wx.getStorageSync('usermsg');
    console.log("hi")
    console.log(token)
    // if (token) {
    //   wx.switchTab({
    //     url: '../../doctor/index/index',
    //   })
    // }
  },

  /**
   * 聚焦事件
   */
  inputFocus: function (t) {
    var a = {};
    a[t.target.dataset.id] = "changed", this.setData(a);
  },

  /**
   * 失焦事件  设置输入框的样式
   */
  inputBlur: function (t) {
    // console.log(t)
    // var a = {};
    // a[t.target.dataset.id] = t.detail.value ? "nick" : "", this.setData(a);
    this.setData({
      [t.target.dataset.id]: t.detail.value.length ? t.detail.value : ""
    });
  },

  phoneInput:function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  passwordInput:function(e){
    this.setData({
      password: e.detail.value
    })
  },

  /**
   * 绑定账号事件
   */
  bind: function (e) {
    var that=this;
    var phone = e.detail.value.phone;
    var password = e.detail.value.password;
    var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    let value = e.detail.value;
    console.log(phone.length)
    
    // 判断 手机号和验证码 是否输入
    if (phone=="") {
      wx.showToast({
        title: '请输入手机号码！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (password=="") {
      wx.showToast({
        title: '请输入密码！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }else{
      wx.showLoading({
        title: '登录中...',
      })
      wx.request({
        url: api.Login,
        // url:"http://127.0.0.1:9001/patients/regist/login",
        data: {
          tel: that.data.phone,
          password: that.data.password
          // phone: that.data.phone,
          // password: that.data.password
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
          // 'Content-Type': 'application/json'
        },
        success: function (res) {
          wx.hideLoading()
          if (res.data.code == 0) {
            app.globalData.hasLogin = true;
            app.globalData.personals=res.data.data;
            wx.showToast({
              title: '登录成功!',
              icon: 'success',
              duration: 2000,
              mask:true,
              success:function(){
                // wx.setStorageSync('usermsg', res.data.data);//存储用户信息
                wx.setStorage({
                  key: "usermsg",
                  data: res.data.data,
                  success: function () {
                    wx.switchTab({
                      url: '../../doctor/index/index'
                    });
                  }
                },);
              }
            })
            
            // wx.setStorageSync('userInfo', res.data.data.userInfo);
            // wx.setStorage({
            //   key: "token",
            //   data: res.data.data.token,
            //   success: function () {
            //     wx.switchTab({
            //       url: '/pages/ucenter/index/index'
            //     });
            //   }
            // });
          } else {
            wx.showModal({
              title: '登录失败，请检查相应信息！',
              content: res.data.msg,
              showCancel: false
            });
          }
        }
      });
     }
  },
  jumptoregister:function(){
    console.log("ks跳转到注册页面")
    wx.navigateTo({
      url: '../registerindex/registerindex',
    })
    console.log("跳转到注册页面")
  },

  
});